package cn.itcast.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XmlUtils {
	private static String filepath;
	static{
		filepath = XmlUtils.class.getClassLoader().getResource("users.xml").getPath(); 
		
	}
	
	public static Document getDocument() throws Exception {
		SAXReader reader = new SAXReader();
        Document document = reader.read(new File(filepath));
        return document;
		
	}
	
	public static void write2Xml(Document document) throws IOException, FileNotFoundException {
		  // Pretty print the document to System.out
        OutputFormat format = OutputFormat.createPrettyPrint();//格式化输入器
        format.setEncoding("UTF-8");
        XMLWriter writer = new XMLWriter( new FileOutputStream(filepath), format );
        writer.write( document );
        writer.close();
		
	}
}
